package com.expand.learn.config;

import com.expand.learn.model.Human;
import com.expand.learn.model.Person;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhanghaidong
 * @version v1.0
 * @description
 * @date 2020/2/26
 */
@Component
public class CustomPersonArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        System.out.println("--------1、CustomPersonArgumentResolver --- supportsParameter ------");
        final Class<?> aClass = methodParameter.getParameterType();
        //controller方法的参数 Human
        return aClass == Human.class;
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);

        String person = request.getParameter("person");
        System.out.println("--------2、CustomPersonArgumentResolver --- resolveArgument ------" + person);
        Human human = new Human();
        final Person person1 = new Person("小明", 100);
        human.setPerson(person1);
        return human;
    }
}

/*
spring 默认的参数转换实现 26种

0 = {RequestParamMethodArgumentResolver@5964}
1 = {RequestParamMapMethodArgumentResolver@5965}
2 = {PathVariableMethodArgumentResolver@5966}
3 = {PathVariableMapMethodArgumentResolver@5967}
4 = {MatrixVariableMethodArgumentResolver@5968}
5 = {MatrixVariableMapMethodArgumentResolver@5969}
6 = {ServletModelAttributeMethodProcessor@5970}
7 = {RequestResponseBodyMethodProcessor@5971}
8 = {RequestPartMethodArgumentResolver@5972}
9 = {RequestHeaderMethodArgumentResolver@5973}
10 = {RequestHeaderMapMethodArgumentResolver@5974}
11 = {ServletCookieValueMethodArgumentResolver@5975}
12 = {ExpressionValueMethodArgumentResolver@5976}
13 = {SessionAttributeMethodArgumentResolver@5977}
14 = {RequestAttributeMethodArgumentResolver@5978}
15 = {ServletRequestMethodArgumentResolver@5979}
16 = {ServletResponseMethodArgumentResolver@5980}
17 = {HttpEntityMethodProcessor@5981}
18 = {RedirectAttributesMethodArgumentResolver@5982}
19 = {ModelMethodProcessor@5983}
20 = {MapMethodProcessor@5984}
21 = {ErrorsMethodArgumentResolver@5985}
22 = {SessionStatusMethodArgumentResolver@5986}
23 = {UriComponentsBuilderMethodArgumentResolver@5987}
24 = {RequestParamMethodArgumentResolver@5988}
25 = {ServletModelAttributeMethodProcessor@5486}


根据controller 具体请求方法的参数所加的注解或者其他进行参数到对象的绑定

 */